package com.whxd.system.authority.service;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.whxd.framework.utils.BeanMapper;
import com.whxd.framework.utils.StringSplit;
import com.whxd.system.authority.entity.Authority;
import com.whxd.system.authority.repository.AuthorityDao;
import com.whxd.system.authority.web.AuthorityDto;

/**
 * 权限服务实现
 * 
 */
@Service
public class AuthorityServiceImpl implements AuthorityService {

	@Autowired
	private AuthorityDao authorityDao;

	public void setAuthorityDao(AuthorityDao authorityDao) {
		this.authorityDao = authorityDao;
	}

	/*
	 * @see com.simple.spark.system.service.authority.AuthorityService#findAll()
	 */
	@Override
	public List<AuthorityDto> findAll() {
		Iterable<Authority> lst = authorityDao.findAll();
		List<AuthorityDto> dtos = BeanMapper.mapList(lst, AuthorityDto.class);
		return dtos;
	}

	/*
	 * @see
	 * com.simple.spark.system.service.authority.AuthorityService#findOne(java
	 * .lang.Long)
	 */
	@Override
	public AuthorityDto findOne(Long id) {
		Authority authority = authorityDao.findOne(id);
		AuthorityDto dto = BeanMapper.map(authority, AuthorityDto.class);
		return dto;
	}

	/*
	 * @see
	 * com.simple.spark.system.service.authority.AuthorityService#add(com.simple
	 * .spark.system.ctrl.authority.AuthorityDto)
	 */
	@Override
	public void add(AuthorityDto dto) {
		Authority authority = BeanMapper.map(dto, Authority.class);
		authorityDao.save(authority);
	}

	/*
	 * @see
	 * com.simple.spark.system.service.authority.AuthorityService#update(com
	 * .simple.spark.system.ctrl.authority.AuthorityDto)
	 */
	@Override
	public void update(AuthorityDto dto) {
		Authority authority = authorityDao.findOne(dto.getId());
		BeanMapper.copy(dto, authority);
		authorityDao.save(authority);
	}

	/*
	 * @see
	 * com.simple.spark.system.service.authority.AuthorityService#deleteBatch
	 * (java.lang.String)
	 */
	@Override
	public void deleteBatch(String ids) {
		for (long id : StringSplit.splitToLong(ids, ",")) {
			authorityDao.delete(authorityDao.findOne(id));
		}
	}

	@Override
	public AuthorityDto findByName(String name) {
		return authorityDao.findByName(name);
	}

}
